import QtQuick 2.0
import QtWebSockets 1.1

Item {
    anchors.fill: parent

    Text {
        id: txt
        anchors.centerIn: parent
        horizontalAlignment: Text.AlignHCenter
        font.pixelSize: 20
    }

    WebSocket {
        id: socket
        url: "wss://echo.websocket.org"
        active: true
        onTextMessageReceived: {
            txt.text = message
        }
        onStatusChanged: {
            if (socket.status === WebSocket.Connecting) {
                txt.text = "[LOADING] " + socket.errorString;
            } else if (socket.status === WebSocket.Error) {
                txt.text = "[ERROR] " + socket.errorString;
            } else if (socket.status === WebSocket.Open) {
                socket.sendTextMessage("Hello World");
            } else if (socket.status === WebSocket.Closed) {
                txt.text += "\n[Socket Close]";
            }
        }
    }
}

